<?php
/**
 * UI管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class AdminController extends UE_Controller_Admin {
	/**
	 * 管理首页
	 *
	 */
	public function indexAction() {
		// 取得菜单
		$menu = array();
		$sql = "SELECT * FROM ue_menu WHERE menu_enabled=1 ORDER BY menu_order ASC";
		$rs = UE_DB::factory()->getAll($sql);
		
		$t = new UE_Tree();
		$t->set($rs, 'menu_id', 'menu_parent_id');
		$menuData = $t->get();
		
		foreach ($menuData as $mk=>$mn) {
			$urlArr = UE_Url::parse($mn['menu_url']);
			//print $mn['menu_url']."\n";
			$urlArr = UE_Url::parse($mn['menu_url']);
			//print_r($urlArr);
			//print "\n";
			//print AppModel::isActionAcessable($urlArr['app'], $urlArr['ctl'], $urlArr['act']);
			//print "\n\n\n";
			
			if ($_SESSION['user_type'] != 'super' && !$mn['isTop']) {
				//*
				$urlArr = UE_Url::parse($mn['menu_url']);
				//print_r($urlArr);
				//print "\n";
				//print AppModel::isActionAcessable($urlArr['app'], $urlArr['ctl'], $urlArr['act']);
				//print "\n\n\n";
				//*/
				// 没有权限则不使用菜单
				if(!AppModel::isActionAcessable($urlArr['app'], $urlArr['ctl'], $urlArr['act'])) {
					continue;
				}
			}
			
			// 取得子菜单id
			$childrenId = array();
			foreach ($mn['chile'] as $chileId) {
				if ($menuData[$chileId]['depth'] == $mn['depth'] + 1) {
					$childrenId[] = $chileId;
				}
			}
			
			$menu[$mk] = array(
			    'menu_name' => $mn['menu_name'],
			    'menu_url' => $mn['menu_url'],
			    'isTop' => $mn['isTop'],
			    'chile' => $childrenId,
			);			
		}
		
		//print_r($menu);exit;
		
		$menuJson = json_encode($menu);		
		
		include $this->tpl();
	}
	
	/**
	 * 后台管理登录
	 *
	 */
	public function loginAction() {
		if (!empty($_SESSION['is_admin_login'])) {
			header('Location: '.UE_Url::make('system/admin/index'));
			return ;
		}
		
		if ($_SESSION['user_type'] != 'admin' && $_SESSION['user_type'] != 'super') {
			header('Location: '.UE_Url::make('system/public/msg/permissionDenied'));
		}
		
		if (IS_POST) {
			if (empty($_POST['password'])) {
				$this->tpl->setErr('密码不能为空。');
			} else {
				$isAdminLoginCaptcha = OptionModel::getOption('USE_ADMIN_LOGIN_CAPTCHA');			
		
				// 验证码						
				if($isAdminLoginCaptcha && !UE_Captcha::check(@$_POST['secode'], 'userLogin')) {
					$this->setErr('验证码错误。');
				} else {
					UE_Loader::loadModel('UserModel', 'user');
					$user = new UserModel();
					if($user->adminLogin($_POST['password'])) {
						$_SESSION['is_admin_login'] = 1;
						// 登录成功,跳转
						if (!empty($_POST['forward'])) {
							header('Location: ' . urldecode($_POST['forward']));
							return;
						}
						
						header('Location: ' . UE::referer());
					} else {
						$this->setErr($user->getErrs());
					}
				}
			}
						
		}
		
		include $this->tpl('admin.login');
	}
	
	/**
	 * 后台欢迎页面
	 *
	 */
	public function welcomeAction() {
		include $this->tpl('admin.welcome');
	}
	
}